/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "GameWorldScene.h"

#include "SceneObject.h"
#include "UISystem.h"
#include "InputSystem.h"

namespace PQClient
{
	GameWorldScene::GameWorldScene() : GameScene(Scene_GameWorld,Ogre::ST_EXTERIOR_CLOSE),
		_uiLoaded(false),_waterManager(0),_boidsManager(0),_vegetationManager(0),
		_playerController(0)
	{


	}

	void GameWorldScene::loadScene()
	{
		//_sceneObject->getRenderProperty().setBackground(Ogre::ColourValue(0,0,0));

		//
		_sceneObject->getRenderProperty().setAmbient(Ogre::ColourValue(0,0,0));
		Ogre::SceneManager* mgr=_sceneObject->getSceneManager();
		mgr->setWorldGeometry("Island.cfg");

		//_skyEffect=new SkyEffect(_sceneObject);

		
		_waterManager=new WaterEffect(_sceneObject);
		_boidsManager=new BoidsManager(_sceneObject);
		_vegetationManager=new VegetationManager(_sceneObject);

		_playerController=new PlayerController(_sceneObject);

	}

	void GameWorldScene::update(float deltaT)
	{
		GameScene::update(deltaT);
		
		if(_waterManager){
			_waterManager->update(deltaT);
		}

		if(_vegetationManager){
			_vegetationManager->update(deltaT);
		}
		if(_boidsManager){
			_boidsManager->update(deltaT);
		}
		
		if(_playerController){
			_playerController->update(deltaT);
		}
		
	}

	bool GameWorldScene::keyPressed( const OIS::KeyEvent &evt )
	{
		
		if(_playerController){
			_playerController->injectKeyDown(evt);
		}
		return true;
	}

	void GameWorldScene::activate()
	{
		GameScene::activate();
		if(!_uiLoaded){
			UISystem::getSingleton().setCurrentScreen(UIScreen::UIScreen_Unkown);

		}

	}

	bool GameWorldScene::keyReleased( const OIS::KeyEvent &e )
	{
		if(_playerController){
			_playerController->injectKeyUp(e);
		}

		return true;
	}

	bool GameWorldScene::mouseMoved( const OIS::MouseEvent &evt )
	{
		if(_playerController){
			float deltaYaw=-0.05f * evt.state.X.rel;
			float deltaPitch=-0.05f * evt.state.Y.rel;
			float deltaZoom=-0.0005f * evt.state.Z.rel;
			_playerController->updateCameraGoal(0, 0, deltaZoom);
			OIS::Mouse* mouse=InputSystem::getSingleton().getMouse();
			if(mouse->getMouseState().buttonDown(OIS::MB_Left)){
				
				_playerController->updateCameraGoal(deltaYaw, deltaPitch, 0);
			}
			
		}
		
		return true;
	}

	bool GameWorldScene::mousePressed( const OIS::MouseEvent &e, OIS::MouseButtonID id )
	{
		return true;
	}

	bool GameWorldScene::mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id )
	{
		return true;
	}

	
	
}
